package com.hspedu.springboot.controller;

import com.sun.deploy.net.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;

@Controller
public class RequestController {
    @GetMapping("/login")
   public String login(HttpServletRequest request){
        request.setAttribute("username","laohan");

       return "forward:/ok";
   }
   @GetMapping("/ok")
   @ResponseBody
   public String ok(@RequestAttribute(value = "username",required = false)String name){
       System.out.println("name-----------"+name);
       return "success";
   }
    @GetMapping("/register")
   public String register(Map<String, Object> map,
                          Model model,
                          HttpServletResponse response){
        map.put("user","hspedu");
        map.put("job","java架构师");
        model.addAttribute("sal",80000);
        Cookie cookie = new Cookie("email", "hspedu@sohu.com");
        response.addCookie(cookie);
        return "forward:/registerOk";
   }
   @GetMapping("/registerOk")
   @ResponseBody
    public String registerOk(HttpServletRequest request){
       System.out.println("user-----"+request.getAttribute("user"));
       System.out.println("job-----"+request.getAttribute("job"));
       System.out.println("sal-----"+request.getAttribute("sal"));
       return "success";
   }
}
